// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Girder Door Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 55 : TYPE_GIRDERDOOR

// Function declarations
reserve function GirderDoor_DebugDraw
reserve function GirderDoor_DebugSpawn
reserve function GirderDoor_PushMonika

// Static Values

// Tables

function GirderDoor_DebugDraw
	DrawSprite(0)
end function


function GirderDoor_DebugSpawn
	CreateTempObject(TypeName[Girder Door], 0, object.xpos, object.ypos)
end function


function GirderDoor_PushMonika
	if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
		if object[0].value1 >= 14
			BoxCollisionTest(C_TOUCH, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				if object[currentPlayer].ypos > object.ypos
					object[currentPlayer].iypos = object[currentPlayer].collisionBottom
					FlipSign(object[currentPlayer].ypos)
					object[currentPlayer].ypos += 0x240000
					object[currentPlayer].ypos += object.ypos
					if object[currentPlayer].yvel < 0
						object[currentPlayer].yvel = 0
					end if
				else
					object[currentPlayer].iypos = object[currentPlayer].collisionTop
					FlipSign(object[currentPlayer].ypos)
					object[currentPlayer].ypos -= 0x240000
					object[currentPlayer].ypos += object.ypos
					if object[currentPlayer].yvel > 0
						object[currentPlayer].yvel = 0
					end if
				end if		
			end if	
		end if	
	end if	
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0		
					BoxCollisionTest(C_BOX, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if	
			CallFunction(GirderDoor_PushMonika)
		next
		break
	case 1
		object.priority = PRIORITY_ACTIVE_BOUNDS
		if object[-1].value0 == 1
			if object.value2 == 0
				object.value2 = 1
				object.state++
			end if
		else
			object.value2 = 0
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0		
					BoxCollisionTest(C_BOX, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if	
			CallFunction(GirderDoor_PushMonika)	
		next
		break
	case 2
		object.xpos += object.xvel
		object.value0++
		if object.value0 == 64
			object.value0 = 0
			object.state++
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0		
				BoxCollisionTest(C_BOX, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object[currentPlayer].xpos += object.xvel
				end if
			end if	
			CallFunction(GirderDoor_PushMonika)	

		next
		break
	case 3
		object.value0++
		if object.value0 == 180
			object.value0 = 0
			object.state++
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0		
					BoxCollisionTest(C_BOX, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if	
			CallFunction(GirderDoor_PushMonika)	
		next
		break
	case 4
		object.xpos -= object.xvel
		object.value0++
		if object.value0 == 64
			object.value0 = 0
			object.state = 1
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0		
				BoxCollisionTest(C_BOX, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object[currentPlayer].xpos -= object.xvel
				end if					
			end if	
			CallFunction(GirderDoor_PushMonika)	
		next
		break
	end switch


end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-64, -12, 128, 24, 383, 140)
	foreach (TypeName[Girder Door], arrayPos0, ALL_ENTITIES)
		switch object[arrayPos0].propertyValue
		case 1
			object[arrayPos0].state = 1
			object[arrayPos0].xvel = 0x20000
			break
		case 2
			object[arrayPos0].state = 1
			object[arrayPos0].xvel = -0x20000
			break
		end switch
		object[arrayPos0].value1 = object[arrayPos0].xpos
	next
	SetTableValue(TypeName[Girder Door], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(GirderDoor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(GirderDoor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
